package com.cgw.game.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.cgw.game.R;

/**
 * @Description TODO
 * @Author YF
 * @Date 2020/8/24
 * @Version 1.0
 */
public class WaveView extends View {
    private Paint mPaint;
    private int width;
    private int height;
    private int waveWidth;
    private int baseLine = 100;
    private float offset = 0;
    private float lowOffset = 0;
    private int waveHeight = 100;

    ValueAnimator mAnimator;


    public WaveView(Context context) {
        this(context, null);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }
    private void initView() {
        mPaint = new Paint();
        mPaint.setColor(Color.RED);
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        width = getMeasuredWidth();//获取屏幕宽度
        height = getMeasuredHeight();//获取屏幕高度
        waveWidth = width;
        updateXControl();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(getResources().getColor(R.color.wave_view_color));
        canvas.drawPath(getPath(offset), mPaint);
        mPaint.setColor(getResources().getColor(R.color.wave_view_colors));
        canvas.drawPath(getPath(lowOffset), mPaint);
    }

    private Path getPath(float offset){
        Path mPath = new Path();
        int itemWidth = waveWidth/2;//半个波长
        mPath.moveTo(-itemWidth * 4, baseLine);//起始坐标
        //核心的代码就是这里
        for (int i = -3; i < 2; i++) {
            int startX = i * itemWidth;
            mPath.quadTo(
                    startX + itemWidth / 2 + offset,//控制点的X,（起始点X + itemWidth/2 + offset)
                    getWaveHeigh(i),//控制点的Y
                    startX + itemWidth + offset,//结束点的X
                    baseLine//结束点的Y
            );//只需要处理完半个波长，剩下的有for循环自已就添加了。
        }
        mPath.lineTo(width, height);
        mPath.lineTo(0, height);
        mPath.close();
        return mPath;
    }
    //奇数峰值是正的，偶数峰值是负数
    private int getWaveHeigh(int num) {
        if (num % 2 == 0) {
            return baseLine + waveHeight - 5;
        }
        return baseLine - waveHeight - 5;
    }
    /**
     * 不断的更新偏移量，并且循环。
     */
    private void updateXControl() {
        //设置一个波长的偏移
        mAnimator = ValueAnimator.ofFloat(0, 1.0f);
        mAnimator.setInterpolator(new LinearInterpolator());
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float animatorValue = (float) animation.getAnimatedValue();
                offset = animatorValue * waveWidth;//不断的设置偏移量，并重画
                lowOffset = animatorValue * waveWidth*3/2;
                postInvalidate();
            }
        });
        mAnimator.setDuration(5000);
        mAnimator.setRepeatCount(ValueAnimator.INFINITE);
        startAnimator();
    }

    public void clearAnimator() {
        if (mAnimator != null && mAnimator.isRunning()) {
            mAnimator.cancel();
        }
    }

    public void startAnimator() {
        if (mAnimator != null && !mAnimator.isRunning()) {
            mAnimator.start();
        }
    }
}
